package com.gopay.common.util;

import java.io.*;

/**
 * Created by ssj on 2017/8/21.
 */
public class ReadWrite {
    /**
     * 读取文件
     * @param readPath
     * @return
     */
    public static final String read(String readPath) {
        String readTxt = "";
        try {
            File f = new File(readPath);
            if (f.isFile() && f.exists()) {
                InputStreamReader read = new InputStreamReader(
                        new FileInputStream(f), "UTF-8");
                BufferedReader reader = new BufferedReader(read);
                String line;
                while ((line = reader.readLine()) != null) {
//                    readTxt +=  (line + "\r\n");
                    readTxt +=  (line );
                }
                read.close();
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return readTxt;
    }

    /**
     * 写入文件
     *
     * @param writePath
     *            文件路径
     */
    public static  final void  write(String excel_Path,String writePath, String writeTxt) {
        try {
            File filemk= new File(excel_Path);
            //创建目录
            if(!filemk.exists()){
                filemk.mkdirs();
            }
            File f = new File(writePath);
//            if (!f.exists()) {
//                f.createNewFile();
//            }
            OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream(f,true), "UTF-8");
            BufferedWriter writer = new BufferedWriter(write);
            writer.write(writeTxt);
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
//        CCbIoReadWrite readWrite = new CCbIoReadWrite();


//        String iptMsg = "";
//        Scanner sc = new Scanner(System.in);
//        System.err.println("请输入内容：");
//        iptMsg = sc.next();
//        System.err.println("接收到内容：" + iptMsg);
//        System.out.println("----开始写文件----");
//        String writePath = "C:/3.txt";
//        readWrite.write(writePath, iptMsg);
//        System.out.println("----结束写文件----");

//        System.out.println("----读取文件开始----");
//        String readPath = "C:/3.txt";
//        String txt = readWrite.read("C:\\excelCcb\\2017-08-09\\CCB_DCBL_2017-08-09.xls");
//        System.out.println("----读取文件结束----"+txt);
//
//        System.out.println("----开始写文件----");
//        String writePathString = "G:/4.txt";
//        readWrite.write(writePathString, txt);
//        System.out.println("----结束写文件----");
    }
}
